﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.UI.BaseDataSvc;
using YY.Common;

namespace YY.UI
{
    public partial class EditCargoFrm : BaseFrm
    {
        public TB_Order Order { get; set; }
        /// <summary>
        /// 货物所属箱号ID
        /// </summary>
        public int CaseID { get; set; }

        /// <summary>
        /// 编辑模式时传入id
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// TB_Cargo临时变量
        /// </summary>
        TB_Cargo _Cargo = null;

        /// <summary>
        /// 通用分类数据临时变量
        /// </summary>
        IEnumerable<TB_Options> options = null;

        /// <summary>
        /// 是否编辑模式
        /// </summary>
        public bool EditMode { get; set; }

        public EditCargoFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new YY.UI.BaseDataSvc.BaseDataClient();
        }

        private void EditCargoFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            this.options = this.BaseDataProxy.FindOptions(null, null, null);
            Common.BindOptionsComboBox(this.cbBoxPackageType, this.options, EnumCommon.OptionType.包装种类);
            this.cbBoxPackageType.KeyPress += new KeyPressEventHandler(optionCbBox_KeyPress);

            if (this.EditMode)
                bindData(this.ID);
        }

        private void bindData(int id)
        {
            this._Cargo = this.BaseDataProxy.GetCargo(id);
            if (_Cargo != null)
            {
                this.txtBoxAmount.Text = this._Cargo.amount;
                this.txtBoxCargoName.Text = this._Cargo.cargoName;
                this.txtBoxCaseAmount.Text = this._Cargo.caseAmount;
                this.txtBoxHairWeight.Text = this._Cargo.hairWeight;
                this.txtBoxMarker.Text = this._Cargo.marker;
                this.txtBoxMemo.Text = this._Cargo.memo;
                this.txtBoxNetWeight.Text = this._Cargo.netWeight;
                this.txtBoxVolume.Text = this._Cargo.volume;
                Common.SelectedOptionsComboBox(this.cbBoxPackageType, this.options, EnumCommon.OptionType.包装种类, this._Cargo.packageType);
                this.CaseID = this._Cargo.@case;
            }
        }

        /// <summary>
        /// 通用分类选择框回车事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void optionCbBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                ComboBox cbBox = sender as ComboBox;
                EnumCommon.OptionType ot = (EnumCommon.OptionType)cbBox.Tag;
                if (this.options != null)
                {
                    TB_Options[] os = this.options.Where(p => p.type == ot.ToString()).Where(p => p.name.Contains(cbBox.Text) || p.code.Contains(cbBox.Text)).ToArray();
                    Common.ComboBoxMatchOption(cbBox, os, this, ot);
                }
            }
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.groupBox1.Controls.Count; i++)
            {
                if (this.groupBox1.Controls[i].Name.StartsWith("txtBox"))
                    this.groupBox1.Controls[i].Text = null;
            }
            this.EditMode = false;
            this.ID = 0;
        }

        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            if (Common.BIZ_OrderIsSubmit(this.Order.id))
            {
                MessageBox.Show("已提交财务审核单据不能修改！");
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxCargoName.Text))
            {
                MessageBox.Show("请输入货名！");
                this.txtBoxCargoName.Focus();
                return;
            }
            TB_Cargo c = new TB_Cargo();
            c.amount = this.txtBoxAmount.Text;
            c.cargoName = this.txtBoxCargoName.Text;            
            c.caseAmount = this.txtBoxCaseAmount.Text;
            c.hairWeight = this.txtBoxHairWeight.Text;            
            c.marker = this.txtBoxMarker.Text;
            c.memo = this.txtBoxMemo.Text;
            c.netWeight = this.txtBoxNetWeight.Text;
            if (this.cbBoxPackageType.SelectedItem != null)
                c.packageType = (this.cbBoxPackageType.SelectedItem as TB_Options).id;
            c.volume = this.txtBoxVolume.Text;

            Common.OperateType opType = Common.OperateType.Add;
            bool ok = false;
            if (this.EditMode)
            {
                c.id = this._Cargo.id;
                c.@case = this._Cargo.@case;
                opType = Common.OperateType.Update;
                ok = this.BaseDataProxy.UpdateCargo(c);
            }
            else
            {
                c.@case = this.CaseID;
                this.ID = this.BaseDataProxy.AddCargo(c);
                ok = this.ID > 0;
                c.id = this.ID;
            }

            if (ok)
            {
                this._Cargo = c;
                this.EditMode = true;
                CommonEventArg<TB_Cargo> arg = new CommonEventArg<TB_Cargo>(c, opType);
                YY.Common.EventCommon.OnCargoUdDGVEvnt(sender, arg);
            }
            else
                MessageBox.Show("保存异常！");
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            if (this.Order.isSubmit)
            {
                MessageBox.Show("已提交财务审核单据不能修改！");
                return;
            }

            if (MessageBox.Show("确认删除该项吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (this.EditMode && this._Cargo!=null)
                {
                    if (this.BaseDataProxy.DeleteCargo(this._Cargo.id))
                    {
                        CommonEventArg<TB_Cargo> arg = new CommonEventArg<TB_Cargo>(this._Cargo, Common.OperateType.Delete);
                        YY.Common.EventCommon.OnCargoUdDGVEvnt(sender, arg);
                        MessageBox.Show("货物资料已删除！");
                        this.Close();
                    }
                    else
                        MessageBox.Show("删除异常！");
                }
            }
        }
    }
}
